package postgres

import (
	"context"
	"gorm.io/gorm"
	"manager/errors"
	"manager/model"
	"manager/model/entity"
)

var Message = &message{}

type message struct{}

// Create 创建
func (a *message) Create(ctx context.Context, m *entity.Message) (int64, error) {
	err := GetDB(ctx).Create(m).Error
	return m.Id, err
}

// Find 查找详情
func (a *message) Find(ctx context.Context, in *model.MessageInfoRequest) (*entity.Message, error) {
	e := &entity.Message{}

	q := GetDB(ctx).Model(&entity.Message{})

	if in.Id > 0 {
		err := q.First(&e, in.Id).Error
		return e, err
	}

	count := 0

	if in.UserId != nil {

		q = q.Where("user_id = ?", in.UserId)

		count++
	}

	if in.PushType != nil {

		q = q.Where("push_type = ?", in.PushType)

		count++
	}

	if in.PushRange != nil {

		q = q.Where("push_range = ?", in.PushRange)

		count++
	}

	if in.PushTime != nil {

		q = q.Where("push_time = ?", in.PushTime)

		count++
	}

	if in.Title != nil {

		q = q.Where("title like ?", in.Title)

		count++
	}

	if in.Content != nil {

		q = q.Where("content like ?", in.Content)

		count++
	}

	if in.Delay != nil {

		q = q.Where("delay = ?", in.Delay)

		count++
	}

	if in.DeletedAt != nil {

		q = q.Where("deleted_at = ?", in.DeletedAt)

		count++
	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

		count++
	}

	if count == 0 {
		return e, errors.New("condition illegal")
	}

	err := q.First(&e).Error
	return e, err
}

// Update 更新
func (a *message) Update(ctx context.Context, id int64, dict map[string]interface{}) error {
	return GetDB(ctx).Model(&entity.Message{}).Where("id = ?", id).Updates(dict).Error
}

// Delete 删除
func (a *message) Delete(ctx context.Context, id int64) error {
	return GetDB(ctx).Delete(&entity.Message{}, id).Error
}

// List 列表查询
func (a *message) List(ctx context.Context, in *model.MessageListRequest) (int, []*entity.Message, error) {
	var (
		q        = GetDB(ctx).Model(&entity.Message{})
		err      error
		total    int64
		messages []*entity.Message
	)

	if in.UserId != nil {

		q = q.Where("user_id = ?", in.UserId)

	}

	if in.PushType != nil {

		q = q.Where("push_type = ?", in.PushType)

	}

	if in.PushRange != nil {

		q = q.Where("push_range = ?", in.PushRange)

	}

	if in.PushTime != nil {

		q = q.Where("push_time = ?", in.PushTime)

	}

	if in.Title != nil {

		q = q.Where("title like ?", in.Title)

	}

	if in.Content != nil {

		q = q.Where("content like ?", in.Content)

	}

	if in.Delay != nil {

		q = q.Where("delay = ?", in.Delay)

	}

	if in.DeletedAt != nil {

		q = q.Where("deleted_at = ?", in.DeletedAt)

	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

	}

	if in.StartTime != nil && in.EndTime != nil {
		q = q.Where("push_time between ? and ?", in.StartTime, in.EndTime)
	}

	if len(in.TimeZone) == 2 {
		q = q.Where("push_time between ? and ?", in.TimeZone[0], in.TimeZone[1])
	}

	if err = q.Count(&total).Error; err != nil {
		return 0, nil, err
	}
	if err = q.Order("created_at desc").Limit(in.Size).Offset((in.Index - 1) * in.Size).Find(&messages).Error; err != nil {
		return 0, nil, err
	}
	return int(total), messages, nil
}

// ExecTransaction db事务执行
func (a *message) ExecTransaction(ctx context.Context, callback func(ctx context.Context) error) error {
	return GetDB(ctx).Transaction(func(tx *gorm.DB) error {
		ctx = context.WithValue(ctx, DBCONTEXTKEY, tx)
		return callback(ctx)
	})
}
